﻿using System;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using O1.Kernel.Storage.Transactions;

namespace Tests.O1.Kernel.Storage.Transactions
{
    [TestClass]
    public class TxExtensionTests
    {
        [TestCategory("Tx")]
        [TestMethod, TestCategory("Unit Test")]
        public void TxEmbedStatusCommittedIsEqualToMaskedId()
        {
            ulong txId = 10220;
            var committed = txId.EmbedStatus(TxStatus.Committed);
            committed.Should().Be(txId.MaskCommitted());
        }

        [TestCategory("Tx")]
        [TestMethod, TestCategory("Unit Test")]
        public void TxEmbedStatusPendingIsLessThanCommitted()
        {
            ulong txId = 10220;
            var pending = txId.EmbedStatus(TxStatus.Pending);
            var committed = txId.EmbedStatus(TxStatus.Committed);
            pending.Should().Be(txId);
            committed.Should().BeGreaterThan(txId);
        }

        [TestCategory("Tx")]
        [TestMethod, TestCategory("Unit Test")]
        public void TxEmbedStatusWithId0PendingIsLessThanCommitted()
        {
            ulong txId = 0;
            var pending = txId.EmbedStatus(TxStatus.Pending);
            var committed = txId.EmbedStatus(TxStatus.Committed);
            pending.Should().Be(txId);
            committed.Should().BeGreaterThan(txId);
        }

        [TestCategory("Tx")]
        [TestMethod, TestCategory("Unit Test")]
        public void TxStatusIsPendingByDefault()
        {
            ((ulong)12220).Status().Should().Be(TxStatus.Pending);
        }

        [TestCategory("Tx")]
        [TestMethod, TestCategory("Unit Test")]
        public void TxStatusFromByteArrayIsPendingByDefault()
        {
            ulong txId = 7958887;
            var buffer = BitConverter.GetBytes(txId);
            buffer.Status().Should().Be(TxStatus.Pending);
        }

        [TestCategory("Tx")]
        [TestMethod, TestCategory("Unit Test")]
        public void TxStatusFromByteArrayIsCommittedAsExpected()
        {
            ulong txId = ((ulong)7958887).EmbedStatus(TxStatus.Committed);
            var buffer = BitConverter.GetBytes(txId);
            buffer.Status().Should().Be(TxStatus.Committed);
        }

        [TestCategory("Tx")]
        [TestMethod, TestCategory("Unit Test")]
        public void TxStatusEmbedsIntoByteArrayAsExpected()
        {
            ulong txId = 7958887;
            var buffer = BitConverter.GetBytes(txId);
            buffer.EmbedStatus(TxStatus.Committed);
            var committedId = BitConverter.ToUInt64(buffer, 0);
            committedId.Status().Should().Be(TxStatus.Committed);
        }
    }
}
